package com.timothy.android;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.util.StopPollingToast;
import com.timothy.android.util.ToastUtil;
import com.timothy.answer.BaseAnswer;
import com.timothy.service.CommunicationService;
import com.timothy.service.MessageSenderUtil;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class BaseQuestionActivity extends BaseProcessActivity {
	private static final String LOG_TAG = "BaseQuestionActivity";
	public LayoutSetting setting = null;
	
	protected int countDownTime = -1;
	protected CustomToast ct = null;
	protected StopPollingToast sPollingToast= null;
	
	//define stopToast and countdownToast
	protected ToastUtil stopToast = null;
	protected ToastUtil countdownToast = null;
	protected MyCount mc = null;
	
	protected Context context = null;
	protected List<View> viewList = new ArrayList<View>();
	
/*    public void appendTitle(String append){
    	this.setTitle(getTitle()+append);
	}*/
    
    TextView questionDesc = null;
    
    /*protected void setHeader(Integer type){
    	Log.i(LOG_TAG,"setHeader()...");
    	Log.i(LOG_TAG,"type:" + type);
    	questionDesc = (TextView) this.findViewById(R.id.questionDesc);
    	if(questionDesc==null){
    		Log.i(LOG_TAG,"questionDesc is null, can not set questionDesc");
    		return;
    	}
		if(type>=1 && type<=5) {
			questionDesc.setText("LayoutA");//2 Buttons Question
		} else if ( type>=6 && type<=10 ) {
			questionDesc.setText("LayoutB");//3 Buttons Question
		} else if ( type>=11 && type<=13 ) {
			questionDesc.setText("LayoutC");//4 Buttons Question
		} else if ( type>=14 && type<=16 ) {
			questionDesc.setText("LayoutD");//5 Buttons Question
		} else if ( type>=17 && type<=21 ) {
			questionDesc.setText("LayoutE");//10 Buttons Question
		} else if ( type>=22 && type<=23 ) {
			questionDesc.setText("LayoutH");//3 Large Buttons Question
		} else if ( type>=24 && type<=25 ) {
			questionDesc.setText("LayoutI");//4 Large Buttons Question
		} else if ( type == 26 ) {
			questionDesc.setText("LayoutX");//Other number of buttons Question
		} else if ( type ==0 ) {
			questionDesc.setText("LayoutX");//Other number of buttons Question
		} else if ( type == 40 ) {
			questionDesc.setText("LayoutMultiSelect");
		} else if ( type == 41  ) {
			questionDesc.setText("LayoutSlide");
		} else if ( type == 42) {
			questionDesc.setText("LayoutPriority");
		} else if ( type == 43 ) {
			questionDesc.setText("LayoutFreeForm");
		} else if (  type == 45) {
			questionDesc.setText("LayoutGrading");
		} else{
			questionDesc.setText("LayoutError");
		}
	}*/
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG,"onCreate()...");
		
		context = this;
		
		//load screen size
		Log.i(LOG_TAG,"Start Load Screen Setting...");
		setting = new LayoutSetting(screenSize,screenHeight,screenHeight);
		
		//set orientation
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i(LOG_TAG,"onDestroy()...");

		if(stopToast!=null){
			stopToast.closeToast();
		}
		if(countdownToast!=null){
			countdownToast.closeToast();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i(LOG_TAG,"onDestroy()...");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i(LOG_TAG,"onDestroy()...");
		if(stopToast!=null){
			stopToast.closeToast();
		}
		if(countdownToast!=null){
			countdownToast.closeToast();
		}
	}
	
	
	
	@Override
	protected void processMessage(int what, String content) {
		Log.i(LOG_TAG,"processMessage() start...");
		super.processMessage(what, content);
		
		//stop toast
		/*if(sPollingToast!=null){
			sPollingToast.stopToast();
		}
		*/
		
/*		if (CommunicationService.MSG_STOP_POLLING == what){
			Toast.makeText(this.getBaseContext(), "Stop Polling", Toast.LENGTH_SHORT).show();
			Log.i(LOG_TAG,"CommunicationService.MSG_STOP_POLLING == what");
			for(int i=0;i<viewList.size();i++){
				View btn = viewList.get(i);
//				btn.setBackgroundResource(R.drawable.btn_disabled);
				btn.setEnabled(false);
			}
		}*/
		
		if (CommunicationService.MSG_STOP_POLLING == what){
			
			//if polling stop, cancel countdownToast,cancel MyCount,display stopToast 
			
			//cancel countdownToast
			if(countdownToast!=null){
				countdownToast.closeToast();
			}
			
			//cancel MyCount
			if(mc!=null){
				mc.cancel();
			}
			
			//display stopToast 
			if(stopToast==null){
				stopToast =new ToastUtil(this,"Stop Polling");
				stopToast.showToast();
			}
			
			//disabled all widgets 
			for(int i=0;i<viewList.size();i++){
				View btn = viewList.get(i);
//				btn.setBackgroundResource(R.drawable.btn_disabled);
				btn.setEnabled(false);
			}
		}
		
		Log.i(LOG_TAG,"processMessage() end.");
	}

	//send answers to server
	public void sendAnswer(BaseAnswer baseAnswer){
		/*if( !MessageSender.sendAnswer(baseAnswer) ) {
			ShowDialog("Can not connect to pc, please confirm PC is running!");
		}*/
		
		String message = MessageSenderUtil.transferAnswerToString(baseAnswer);
		if(!sendMessage(message)){
			ShowDialog("Can not connect to pc, please confirm PC is running!");
		}
	}
	
	//倒计时内部类
	class MyCount extends CountDownTimer { 
		List<View> viewList = new ArrayList<View>();
		
		public MyCount(long millisInFuture, long countDownInterval,List<View> btnList) {   
			super(millisInFuture, countDownInterval);   
			this.viewList = btnList;
		}   
		@Override   
		public void onFinish() {   
			for(int i=0;i<viewList.size();i++){
				View btn = viewList.get(i);
				btn.setEnabled(false);
			}
			if(countdownToast!=null){
				countdownToast.closeToast();
			}
		}   
		@Override   
		public void onTick(long millisUntilFinished) {
			Log.i(LOG_TAG,"onTick()...");
			Log.i(LOG_TAG,"time left:"+millisUntilFinished / 1000);
			if(countdownToast!=null){
				countdownToast.closeToast();
			}
			countdownToast = new ToastUtil(context,millisUntilFinished / 1000 + "S");
			countdownToast.showToast();
		}
	}
	
	class CustomToast {
		private int showCount = 0;
		private Toast toast = null;
		private Context context;
		private Handler handler = null;
		
		private Runnable toastThread = new Runnable() {
			public void run() {
				// 递增的count明显地表明是不断运行新的Toast.show()的结果。
				toast.setText(String.valueOf(showCount--) + " S");
				toast.show();
				// 3.3秒后再度重启，设为4s的话将会看到Toast是断断续续地显示着的。
				handler.postDelayed(toastThread, 1000);
			}
		};
		public CustomToast(Context context,int time) {
			this.context = context;
			showCount = time;
			handler = new Handler(this.context.getMainLooper());
			toast = Toast.makeText(this.context, "", Toast.LENGTH_LONG);
		}
		public void setText(String text) {
			toast.setText(text);
		}
		public void showToast(final long length) {
			handler.post(toastThread);
			Thread timeThread = new Thread() {
				public void run() {
					try {
						Thread.sleep(length);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					CustomToast.this.stopToast();
				}
			};
			timeThread.start();
		}
		public void stopToast() {
			// 删除Handler队列中的仍处理等待的消息元素删除
			handler.removeCallbacks(toastThread);
			// 撤掉仍在显示的Toast
			toast.cancel();
		}
	}	
	
	protected void startCountDown(int countDownTime,List<View> viewList){
		if(countDownTime!= -1&&countDownTime>0){
//			ct = new CustomToast(this.getApplicationContext(),countDownTime);
//			ct.showToast(countDownTime * 1000);
			Log.i(LOG_TAG,"countDownTime:"+countDownTime * 1000);
			mc = new MyCount(countDownTime * 1000,1000,viewList);
			mc.start();
		}
	}
	
}
